#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <iostream>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include "../cunchu_quanju.h"
//客戶機類
class Cont
{
private:
    int server_id = 0;               //socket文件描述符
    struct sockaddr_in server_add; 
    char buf[4096];

public:

    //根據ip地址連接服務器
    void lianjie(const string& ip){
        server_id = socket(AF_INET,SOCK_STREAM,0);
        server_add.sin_port = htons(PORT);
        server_add.sin_family = AF_INET;
        server_add.sin_addr.s_addr = inet_addr(ip.c_str());
        if(connect(server_id, (struct sockaddr* )&server_add, sizeof(server_add) == -1)) throw "connect失败"; 
    }

    //向服务器发送消息，出错返回-1
    int send_xiaoxi(const string& str,const string& ip){
        lianjie(ip);
        send(server_id, ip.c_str(), ip.length()+1,0);
        int recv_len = recv(server_id, buf, sizeof(buf),0);
        string recv_str(buf,recv_len);
        return if_ok(recv_str);
    }

    //判断服务器是否返回ok信息
    int if_ok(const string& str){
        return 0;
    }

     virtual ~Cont(){
       if(server_id != 0) close(server_id);
    }
};
